package com.hk.elseservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

import com.hk.commons.CommonResult;
import com.hk.elseservice.service.OnlineMessageService;
import com.hk.model.OnlineMessage;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/5 11:32
 * @Statement: 在线留言
 */
@Slf4j
@RestController
@RabbitListener(queues = {"EmailQueue"})
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private OnlineMessageService onlineMessageService;

    @GetMapping("/findAll")
    public ResultVO<OnlineMessage> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10")Integer limit,
            OnlineMessage onlineMessage
    ) {
        List<OnlineMessage> onlineMessageList = onlineMessageService.findAll(page, limit, onlineMessage);
        PageInfo<OnlineMessage> onlineMessagePageInfo = new PageInfo<>(onlineMessageList);
        int count = onlineMessageService.count(-1,-1, onlineMessage);
        if (onlineMessageList.isEmpty()) {
            ResultVO resultVO = CommonResult.nullResultVO(count,onlineMessagePageInfo.getList());
            return resultVO;
        }
        ResultVO resultVO = CommonResult.successResultVO(count,onlineMessagePageInfo.getList());
        return resultVO;
//        return null;
    }

    /**
     * 开始监听 EmailQueue
     * @param message
     * @return
     */
    @RabbitHandler
    public void save(String message) {
        if (message == null || message == "" ) {
            Result result = CommonResult.nullResult("队列暂未有消息");
            log.error(result.toString());
        } else {
            OnlineMessage onlineMessage = JSONObject.parseObject(message, OnlineMessage.class);
            onlineMessageService.save(onlineMessage);
            Result result = CommonResult.successResult("留言邮箱保存成功");
            log.error("返回信息："+result);
            log.error("队列消息："+message);
        }
//        return null;
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id) {
        OnlineMessage onlineMessage = onlineMessageService.findById(id);
        if (onlineMessage.getId() == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(onlineMessage);
        return result;
//        return null;
    }

    @PostMapping("/updateMessage")
    public Result updateMessage(@RequestBody(required = false) OnlineMessage onlineMessage) throws IOException {
        if (onlineMessage == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        onlineMessageService.update(onlineMessage);
        Result result = CommonResult.successResult(null);
        return result;
    }
    
    @PostMapping("/deletetMessage/{id}")
    public Result deletetMessage(@PathVariable Integer id) throws IOException {
        onlineMessageService.delete(id);
        Result result = CommonResult.successResult(null);
        return result;
    }



}
